package com.grass.cropTradingCenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grass.cropTradingCenter.pojo.entity.Image;
import com.grass.cropTradingCenter.pojo.po.QueryImage;
import com.grass.cropTradingCenter.service.ImageService;
import com.grass.cropTradingCenter.mapper.ImageMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author zipan
* @description 针对表【image】的数据库操作Service实现
* @createDate 2024-03-05 13:30:38
*/
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image>
    implements ImageService{


    @Override
    public List<String> getImageList(QueryImage queryImage) {
        LambdaQueryWrapper<Image> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Image::getOtherId, queryImage.getOtherId())
                .eq(Image::getType, queryImage.getType())
                .orderByAsc(Image::getImgId);
        List<Image> list = this.list(queryWrapper);
        return list.stream().map(Image::getImgUrl).collect(Collectors.toList());
    }

    @Override
    public void removeImages(QueryImage vo) {
        LambdaQueryWrapper<Image> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Image::getOtherId, vo.getOtherId())
                .eq(Image::getType, vo.getType());
        this.remove(queryWrapper);
    }

    @Override
    public void saveImages(QueryImage vo, List<String> images) {
        List<Image> imgs = images.stream().map((imageUrl) -> {
            Image image = new Image();
            image.setImgUrl(imageUrl);
            image.setOtherId(vo.getOtherId());
            image.setType(vo.getType());
            return image;
        }).collect(Collectors.toList());
        this.saveBatch(imgs);
    }
}




